using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.Model;
using MicroMsg.UI.Page;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
namespace MicroMsg.UI.UserContrl
{
	public class BottleSessionItem : UserControl, MMItemCtrl, BaseHeadImgItem
	{
		public delegate void ListItemClick(object sender, object object_0);
		private const string TAG = "BottleSessionItem";
		private const int TYPE_NORMAL = 0;
		private const int TYPE_UNREAD_VOICE = 1;
		public static DependencyProperty ListItemProperty = DependencyProperty.Register("ItemObject", typeof(BConversation), typeof(BottleSessionItem), new PropertyMetadata(null, new PropertyChangedCallback(BottleSessionItem.OnItemPropertyChanged)));
		private long ShowTimer = -1L;
		private static Brush[] infoBrush = new Brush[]
		{
			R.theme.LISTA_SIGNATURE_WORD_BRUSH,
			R.theme.LISTA_SIGNATURE_UNREAD_WORD_BRUSH
		};
		public static HeadImgLoadService mHeadImgLoadService = new HeadImgLoadService(true);
		internal MenuItem DeleteBottleMenu;
		internal MenuItem DeleteMsgMenu;
		internal Image HeadImg;
		internal DynamicTextBlock TitleTxt;
		internal TextBlock InfoPre;
		internal SmileyText InfoContent;
		internal TextBlock TimeTxt;
		internal Grid UnreadArea;
		internal Image bkImg;
		internal TextBlock UnreadTxt;
		private bool _contentLoaded;
		public event BottleSessionItem.ListItemClick ItemClick;
		public BConversation ItemObject
		{
			get
			{
				return (BConversation)base.GetValue(BottleSessionItem.ListItemProperty);
			}
			set
			{
				base.SetValue(BottleSessionItem.ListItemProperty, value);
			}
		}
		public string UserName
		{
			get
			{
				return BContactMgr.toBottleContactUserName(this.ItemObject.strUsrName);
			}
			set
			{
			}
		}
		public BottleSessionItem()
		{
			base.add_Loaded(new RoutedEventHandler(this.BottleSessionItem_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.BottleSessionItem_Unloaded));
			this.InitializeComponent();
			this.InitBindResource();
		}
		private void BottleSessionItem_Loaded(object sender, RoutedEventArgs e)
		{
			BottleSessionItem.mHeadImgLoadService.Add(this);
			this.RefreshTimerTxt();
			base.SetValue(TiltEffect.IsTiltEnabledProperty, true);
		}
		private void BottleSessionItem_Unloaded(object sender, RoutedEventArgs e)
		{
			BottleSessionItem.mHeadImgLoadService.Delete(this);
		}
		private void InitBindResource()
		{
			this.DeleteBottleMenu.Header = strings.Float_Bottle_DeleteBottle;
			this.DeleteMsgMenu.Header = strings.Float_Bottle_Delete_Conversation;
			this.DeleteBottleMenu.set_Foreground(R.theme.CONTEXTMENU_FOREGROUND_BRUSH);
			this.DeleteMsgMenu.set_Foreground(R.theme.CONTEXTMENU_FOREGROUND_BRUSH);
			this.TitleTxt.set_FontFamily((FontFamily)Application.get_Current().get_Resources().get_Item("PhoneFontFamilySemiLight"));
			this.TitleTxt.set_Foreground(R.theme.LISTA_NAME_WORD_BRUSH);
			this.InfoPre.set_FontFamily((FontFamily)Application.get_Current().get_Resources().get_Item("PhoneFontFamilySemiLight"));
			this.InfoContent.FontFamily = (FontFamily)Application.get_Current().get_Resources().get_Item("PhoneFontFamilySemiLight");
			this.TimeTxt.set_FontFamily((FontFamily)Application.get_Current().get_Resources().get_Item("PhoneFontFamilySemiLight"));
			this.bkImg.set_Source(new ImageSourceConverter().ConvertFromString(R.theme.plugin_qqoffline_unreadnum_bk) as ImageSource);
			this.UnreadTxt.set_Foreground(R.theme.SETTING_ITEM_TITLE_BRUSH);
		}
		private void Button_Click(object sender, RoutedEventArgs e)
		{
			if (this.ItemClick != null)
			{
				this.ItemClick(this, this.ItemObject);
			}
		}
		private static void OnItemPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == BottleSessionItem.ListItemProperty)
			{
				(dependencyObject_0 as BottleSessionItem).OnListItemPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void OnListItemPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			try
			{
				this.RefreshItem(dependencyPropertyChangedEventArgs_0.get_NewValue() as BConversation);
			}
			catch (Exception ex)
			{
				Log.smethod_1(base.GetType().get_Name(), ex.get_StackTrace());
			}
		}
		private void RefreshItem(BConversation item)
		{
			if (item == null)
			{
				return;
			}
			this.TitleTxt.Text = item.strNickName;
			this.TimeTxt.set_Text(Util.formatTimeInList(item.nTime, true));
			this.ShowTimer = item.nTime;
			this.InfoContent.Text = "";
			this.InfoPre.set_Text("");
			if (this.getInfoShowColorType(item) == 0)
			{
				string text = "";
				string text2 = BConversationMgr.parseBMsgContentToShow(item, ref text);
				if (text != null)
				{
					this.TitleTxt.Text = text;
				}
				this.InfoContent.Text = text2;
				Binding binding = new Binding("LISTA_SIGNATURE_WORD_BRUSH");
				binding.set_Source(R.theme);
				this.InfoContent.SetBinding(SmileyText.ForegroundProperty, binding);
			}
			else
			{
				string text3 = "";
				string text4 = BConversationMgr.parseBMsgContentToShow(item, ref text3);
				this.TitleTxt.Text = text3;
				if (!string.IsNullOrEmpty(text4))
				{
					this.InfoContent.Text = text4;
					Binding binding2 = new Binding("COMMON_BRUSH");
					binding2.set_Source(R.theme);
					this.InfoContent.SetBinding(SmileyText.ForegroundProperty, binding2);
				}
				if (this.InfoPre.get_ActualWidth() == 0.0)
				{
					this.InfoPre.InvalidateMeasure();
				}
				this.InfoContent.set_MaxWidth(Math.Max(0.0, 245.0 - this.InfoPre.get_ActualWidth()));
			}
			if (item.nUnReadCount > 0)
			{
				if (item.nUnReadCount >= 100)
				{
					this.UnreadTxt.set_Text("...");
				}
				else
				{
					this.UnreadTxt.set_Text(item.nUnReadCount.ToString());
				}
				this.UnreadArea.set_Visibility(0);
			}
			else
			{
				this.UnreadArea.set_Visibility(1);
			}
			string gparam_ = BContactMgr.toBottleContactUserName(this.ItemObject.strUsrName);
			BottleContact bottleContact = StorageMgr.bContact.method_0(gparam_);
			if (bottleContact != null)
			{
				this.HeadImg.set_Source(HeadImgMgr.getCacheHeadImage(bottleContact));
			}
		}
		private void RefreshHeadImg()
		{
			if (string.IsNullOrEmpty(this.ItemObject.strUsrName))
			{
				return;
			}
			string gparam_ = BContactMgr.toBottleContactUserName(this.ItemObject.strUsrName);
			BottleContact bottleContact = StorageMgr.bContact.method_0(gparam_);
			if (bottleContact != null)
			{
				Log.smethod_5("BottleSessionItem", "headImgService has get head img: " + bottleContact.strCity + " " + bottleContact.strUserName);
				ImageSource headImage = HeadImgMgr.getHeadImage(bottleContact);
				if (headImage != this.HeadImg.get_Source())
				{
					this.HeadImg.set_Source(headImage);
				}
			}
		}
		public void RefreshTimerTxt()
		{
			if (this.ShowTimer < 0L)
			{
				return;
			}
			this.TimeTxt.set_Text(Util.formatTimeInList(this.ShowTimer, true));
		}
		private int getInfoShowColorType(BConversation item)
		{
			if (item == null)
			{
				return 0;
			}
			int msgType = MsgInfoObject.getMsgType(item.strMsgType);
			int result = 0;
			if (item.nIsSend == 0)
			{
				result = 0;
				if (msgType == 34 && item.nStatus == 0)
				{
					result = 1;
				}
			}
			return result;
		}
		public void HeadImgLoaded()
		{
			this.RefreshHeadImg();
		}
		public void ScreenIn()
		{
		}
		public void ScreenOut()
		{
		}
		public void Refresh()
		{
			this.RefreshItem(this.ItemObject);
		}
		private void OnDelBottleConvHandler(DlgReturnEventArg dlgReturnEventArg_0)
		{
			switch (MMMessageBox.GetUserClickResult(dlgReturnEventArg_0))
			{
			case 0:
				BConversationMgr.delBottle(this.ItemObject.strUsrName);
				break;
			case 1:
				break;
			default:
				return;
			}
		}
		private void DelBottleMenuItem_Click(object sender, RoutedEventArgs e)
		{
			MMMessageBox mMMessageBox = MMMessageBox.Show(strings.Float_Bottle_Delete_Bottle, strings.Float_Bottle_DeleteBottle_Tip, true, strings.COM_Confirm, strings.COM_Cancel);
			mMMessageBox.ReturnEvent = new DlgReturnEvent(this.OnDelBottleConvHandler);
		}
		private void DelMsgMenuItem_Click(object sender, RoutedEventArgs e)
		{
			BConversationMgr.delBottleMsg(this.ItemObject.strUsrName);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/DriftBottle/UI/BottleSessionItem.xaml", 2));
			this.DeleteBottleMenu = (MenuItem)base.FindName("DeleteBottleMenu");
			this.DeleteMsgMenu = (MenuItem)base.FindName("DeleteMsgMenu");
			this.HeadImg = (Image)base.FindName("HeadImg");
			this.TitleTxt = (DynamicTextBlock)base.FindName("TitleTxt");
			this.InfoPre = (TextBlock)base.FindName("InfoPre");
			this.InfoContent = (SmileyText)base.FindName("InfoContent");
			this.TimeTxt = (TextBlock)base.FindName("TimeTxt");
			this.UnreadArea = (Grid)base.FindName("UnreadArea");
			this.bkImg = (Image)base.FindName("bkImg");
			this.UnreadTxt = (TextBlock)base.FindName("UnreadTxt");
		}
	}
}
